-- Pierwotny właściciel i twórca kodu: AmaMa
-- Zakaz używania / edycji kodu bez wcześniejszej zgody ze strony właściciela
-- Zakaz wykorzystywania całego lub fragmentów kodu
-- W przypadku złamania powyzszych zasad sprawa zostanie skierowana do sądu pod zarzutem: Naruszenia majątkowych praw autorskich
-- Pierwotnie pełne prawo do używania tego kodu posiada tylko AmaMa
local sw,sh = guiGetScreenSize()
local px,py = sw/1366,sh/768
loadstring(exports.dgs:dgsImportFunction())()

cmtaPRZECHOWALNIA = false
local sphere=createColCuboid(-2051, 467.58, 34.17, 7,5,3)
local sphere2=createColCuboid(-2051, 461.58, 34.17, 7,5,3)

addEvent("przechowalnia:showGUI", true)
addEventHandler("przechowalnia:showGUI", resourceRoot, function()
    showCursor(true)
	cmtaPRZECHOWALNIA = true
	window = dgsCreateWindow(1100*px,300*py,250*px,400*py,"Przechowalnia",false)
	dgsWindowSetCloseButtonEnabled(window, false)
	dgsWindowSetMovable (window, false )
	dgsWindowSetSizable (window, false )
	gridlist = dgsCreateGridList (20*px, 30*py, 210*px, 200*py, false,window )
    dgsGridListAddColumn( gridlist, "ID", 0.3 ) 
	dgsGridListAddColumn( gridlist, "Model", 0.6 ) 
	button = dgsCreateButton( 25*px, 260*py, 200*px, 40*py, "Wybierz", false,window,false,false,false,false,false,false,false,tocolor(90,90,90),tocolor(255,0,0,90))
	button2 = dgsCreateButton( 25*px, 310*py, 200*px, 40*py, "Zamknij", false,window,false,false,false,false,false,false,false,tocolor(90,90,90),tocolor(255,0,0,90))	
	addEventHandler ("onDgsMouseClick", getRootElement(), onclick)
	triggerServerEvent("onParkingGetRekords", localPlayer)
	setElementFrozen(localPlayer,true)
end)

addEvent("onParkingGetVehicles", true)
addEventHandler("onParkingGetVehicles", root, function(result)
	setCameraMatrix(-2045.18, 473.93, 301.17+2,-2052.04, 466.40, 301.17)
	object_garage = createObject ( 2469, -2045.39, 472.34, 300.17, 0, 0, 0 )
    setElementDimension(object_garage,getElementData(localPlayer,"player:data").uid)
	setElementDimension(localPlayer,getElementData(localPlayer,"player:data").uid)
	if result then
	for i,v in pairs(result) do
		local row = dgsGridListAddRow ( gridlist )
		dgsGridListSetItemText ( gridlist, row, 1, v["id"] )
		dgsGridListSetItemText ( gridlist, row, 2, getVehicleNameFromModel(v["model"]) )
        dgsGridListSetItemColor ( gridlist, row, 2, 153, 51, 255 )
        dgsGridListSetItemColor ( gridlist, row, 1, 255, 255, 255 )
	end
    end
end)

addEvent("createvehicle:parkingresult", true)
addEventHandler("createvehicle:parkingresult", resourceRoot, function(result)
	for i,v in pairs(result) do
	local color=split(v["color"], ",")
	show_vehiclep = createVehicle(tonumber(v["model"]),-2049.18, 466.93, 301.17,0,0,0)
	setVehicleColor(show_vehiclep, color[1], color[2], color[3], color[4],color[5], color[6], color[7], color[8],color[9], color[10], color[11], color[12])
	setVehiclePlateText(show_vehiclep, tostring(""..v["id"]))
    setVehiclePaintjob(show_vehiclep, v["paintjob"])
	for i2,v2 in ipairs(split(v["tuning"], ",")) do addVehicleUpgrade(show_vehiclep, v2) end
	for i2,v2 in ipairs(split(v["panelstates"], ",")) do setVehiclePanelState(show_vehiclep, i2, tonumber(v2)) end
	end
end)

addEvent("przechowalnia:paintjob", true)
addEventHandler("przechowalnia:paintjob", resourceRoot, function(id,veh)
	vehTexture = dxCreateTexture( ":cmta-paintjoby/files/"..id..".png" )
	shader_carsstart, tec = dxCreateShader ( ":cmta-shadery/shader.fx" )
	engineRemoveShaderFromWorldTexture ( shader_carsstart, "vehiclegrunge256", veh )
	engineApplyShaderToWorldTexture ( shader_carsstart, "vehiclegrunge256", veh )
	dxSetShaderValue ( shader_carsstart, "TX0", vehTexture ) 	
end)

function onclick(btn,state)
	if btn=="left" and state=="down" and cmtaPRZECHOWALNIA then
	if source == button then
		local selected = dgsGridListGetSelectedItem(gridlist)
		if selected == -1 then return end
		local vehicles=getElementsWithinColShape(sphere,"vehicle")
		local vehicles2=getElementsWithinColShape(sphere2,"vehicle")
		if #vehicles == 0 then
		local id= dgsGridListGetItemText(gridlist,selected,1)
		dgsGridListRemoveRow ( gridlist, selected )
		triggerServerEvent("onParkingVehicleSpawn", localPlayer, tonumber(id),"1")
		hidegui()
		return
		end
		if #vehicles2 == 0 then
		local id= dgsGridListGetItemText(gridlist,selected,1)
		dgsGridListRemoveRow ( gridlist, selected )
		triggerServerEvent("onParkingVehicleSpawn", localPlayer, tonumber(id),"2")
		hidegui()
		else
		outputChatBox("Nie ma miejsca, aby wyciągnąć pojazd!",255,0,0)
		return
		end
	end
	if source == button2 then
	hidegui()
	end
	if source == gridlist then 
		local selected = dgsGridListGetSelectedItem(gridlist)
		if selected == -1 then return end
		local id= dgsGridListGetItemText(gridlist,selected,1)
		if not (tonumber(id)) then return end
		triggerServerEvent("createvehicles:parking", resourceRoot,id)
	end
end
end

function hidegui()
        showCursor(false)
        cmtaPRZECHOWALNIA = false
		removeEventHandler ( "onDgsMouseClick", getRootElement(), onclick)
        destroyElement(window)
        setElementFrozen(localPlayer,false)
		setPlayerHudComponentVisible("radar",true)
		setCameraTarget(localPlayer,localPlayer)	
		setElementDimension(localPlayer,0)
	   -- exports["PSSM-interiory"]:addloading(localPlayer,"1")
	    triggerServerEvent("przechowalnia:hideGUI", resourceRoot)
end